#include "stdafx.h"

#include "MacroUtil.h"
#include "WinUtil.h"

#include "DropEdit.h"

LRESULT CDropEdit::OnDropFiles(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
    HRESULT hResult = E_FAIL;

    HDROP hDrop = NULL;
    DWORD dwPathSize = 0;
    WCHAR* wszPathName = NULL;

    hDrop = (HDROP)wParam;

    // We only care about the first file.
    dwPathSize = DragQueryFile(hDrop, 0, NULL, 0);
    ERROR_ESCAPE(dwPathSize <= 0, 0, L"DragQueryFile");        

    ALLOCATE_MEMORY(wszPathName, WCHAR, dwPathSize + 1);

    dwPathSize = DragQueryFile(hDrop, 0, wszPathName, dwPathSize + 1);
    ERROR_ESCAPE(dwPathSize <= 0, 0, L"DragQueryFile");        

    this->SetWindowText(wszPathName);

    hResult = S_OK;

exit:
    // Free the memory block containing the dropped-file information
    DragFinish(hDrop);
    FREE_MEMORY(wszPathName);

    if (hResult != S_OK)
        ShowErrorMessage(m_hWnd, hResult);

    return 0;
}
